const {dialog} = require('electron')
const fs = require('fs');
const path = require('path');


exports.getOpenFilePath =async function () {

     const result = await dialog.showOpenDialog({
         properties: ['openFile']
     })

     const fileName = result && result.filePaths ? result.filePaths[0] : 'No file selected';
     console.log('Selected file:', fileName);
     return fileName;
}

exports.readFile = async function (filePath) {
    try {
        const fullPath = path.resolve(filePath);
        const data = fs.readFileSync(fullPath, 'utf8');
        return JSON.parse(data);
    } catch (err) {
        console.error('Error reading JSON file:', err);
        return { error: err.message };
    }
}

exports.writeFile = async function (filePath, data) {
    console.log('writeFile filePath', filePath,);
    console.log('writeFile data', data,);
    try {
        const fullPath = path.resolve(filePath);
        fs.writeFileSync(fullPath, data, 'utf8');
        return true;
    } catch (err) {
        console.error('Error writing JSON file:', err);
        return false;
    }
}